package com.sxm.yiwei.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.CryActivity;
import com.sxm.yiwei.beans.CryBookSq;
import com.sxm.yiwei.tools.CastTime;

import java.util.HashMap;
import java.util.List;

/**
 * Created by KuKa on 2017/6/19.
 * 抢单列表
 */

public class CryBookAdapter extends BaseAdapter {
   public List<CryBookSq> mList;
   private Context context;
    private Callback mCallback;

    public void setOnClick(Callback callback) {
        this.mCallback=callback;
    }


    public interface  Callback{
        void onclick(int v);
        void onclickItem(int v);

    }



    public CryBookAdapter(Context context, List<CryBookSq> mList) {
        this.context=context;
        this.mList=mList;
    }

    @Override
    public int getCount() {
        return mList.size()==0?0:mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View view;
        ViewHolder holder;
        if (convertView==null){
            view= LayoutInflater.from(context).inflate(R.layout.crybook_item,null);
            holder = new ViewHolder();
           holder.price = (TextView) view.findViewById(R.id.cryitem_price);
            holder.code = (TextView) view.findViewById(R.id.cryitem_code);
            holder.intro = (TextView) view.findViewById(R.id.cryitem_intro);
            holder.time = (TextView) view.findViewById(R.id.cryitem_time);
            holder.type = (TextView) view.findViewById(R.id.cryitem_type);
            holder.crybt = (Button) view.findViewById(R.id.cryitem_crybt);
            holder.cryitem = (RelativeLayout) view.findViewById(R.id.crybook_item);
            view.setTag(holder);


        }else {
            view=convertView;
            holder = (ViewHolder) view.getTag();
        }

        int type = mList.get(position).getType();
        if (type==1){
            holder.type.setText("一键发单");
        }else if (type==2){
            holder.type.setText("急件要发");
        }else {
            holder.type.setText("跑腿劳务悬赏");
        }
        holder.time.setText(CastTime.CastTime(Long.valueOf(mList.get(position).getPublishTime())));
        holder.intro.setText(mList.get(position).getIntro());
        holder.code.setText(mList.get(position).getOrderCode());
        holder.price.setText(mList.get(position).getTotalAmount());
        holder.crybt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallback!=null){
                    mCallback.onclick(position);
                }
            }
        });

        holder.cryitem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallback!=null){
                    mCallback.onclickItem(position);
                }
            }
        });

        notifyDataSetChanged();
        return view;
    }
    class ViewHolder{
        private TextView time,intro,type,code,price;
        private Button crybt;
        RelativeLayout cryitem;
    }
}
